//
//  BaseViewController.swift
//  NewLndd
//
//  Created by 邵禹曦 on 2019/12/31.
//  Copyright © 2019 辽宁当代. All rights reserved.
//

import UIKit
import Hero

class BaseViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //setupNavigationItems()

    }
    
    
    // MARK: - 懒加载属性
    lazy var visitorView : VisitorView  = VisitorView.visitorView()
    
    // MARK: - 定义变量
    var isLogin : Bool = UserTool.shareIntance.isLogin
    
    // MARK: - 临时用的属性
    // 未登陆
    let noLogin : String? = "-102130040002"
    let noLogin4004 :String = "-102130040004"
    
    /*
    
    // MARK: - 系统回调函数
    override func loadView() {
        print(isLogin)
        // 判断要加载哪个view
        isLogin ? super.loadView() : setupVisitorView()
    }*/
    override func loadView() {
           super.loadView()
    }
    
}


// MARK: - 设置UI界面
extension BaseViewController {
    
    // MARK: - 设置访客视图
    func setupVisitorView() {
        //visitorView.backgroundColor = UIColor.purple
        view = visitorView
        setupNavigationItems()
        // 获取访客视图按钮
        visitorView.goLoginBtn.addTarget(self, action: #selector(goLoginBtnClick), for: .touchUpInside);
    }
    
    /// 设置导航栏左右的item
    private func setupNavigationItems() {
        
        // 左上
        //navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(goRegisterBtnClick))
        
        // 右上
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登陆", style: .plain, target: self, action: #selector(goLoginBtnClick))
    }
}


// MARK: - 事件监听
extension BaseViewController {
    
    // MARK: - 登陆
    @objc private func goLoginBtnClick() {
        
        // 1.创建登陆控制器
        let loginVc = LoginViewController()
        
        // 2.包装导航栏控制器
        let loginNav = UINavigationController(rootViewController: loginVc)
        // 全屏跳转
        loginNav.modalPresentationStyle = UIModalPresentationStyle.fullScreen
        
        // 左右横跳
        loginNav.hero.isEnabled = true
        loginNav.hero.modalAnimationType = .selectBy(presenting: .pull(direction: .left), dismissing: .uncover(direction: .right))
        
        
        // 3.弹出控制器
        self.present(loginNav, animated: true, completion: nil)
    }
    

    
}

// MARK: - 自定义函数
@available(iOS 13.0, *)
extension BaseViewController {
    //MARK: - ALert跳转
    @objc func showMsgbox(_message: String, _title: String = "提示") {
        
        let alert = UIAlertController(title: _title, message: _message, preferredStyle: UIAlertController.Style.alert)
        let btnOK = UIAlertAction(title: "好的", style: .default, handler: nil)
        alert.addAction(btnOK)
        self.present(alert, animated: true, completion: nil)
    }
    
    //MARK: - ALert跳转 成功
    @objc func showMsgboxOK(_message: String, _title: String = "提示" , url : String, name : String) {
        let alert = UIAlertController(title: _title, message: _message, preferredStyle: UIAlertController.Style.alert)
        let btnOK = UIAlertAction(title:"好的", style: .default) { (cancelAction)in
            print("ok")
            // 成功跳转
            self.goBack(identifier: url, name: name)
        }
           alert.addAction(btnOK)
           self.present(alert, animated: true, completion: nil)
       }
    
    /// 跳转
    @objc func goBack(identifier: String,name : String)
    {
        let login = UIStoryboard.init(name: name, bundle: nil)
        let play = login.instantiateViewController(identifier: identifier)
                 
        // self.navigationController?.pushViewController(play, animated: true)
        self.present(play, animated: true, completion: nil)
    }
    
    // json串 转数组
    func getDictionaryFromJSONString(jsonString:String) ->NSArray{
     
        let jsonData:Data = jsonString.data(using: .utf8)!
     
        let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
        if dict != nil {
            return dict as! NSArray
        }
        return NSArray()
         
    }
    
    @objc func _alertVisitorView() {
        let alert = UIAlertController(title: "提示！", message: "请登陆！", preferredStyle: UIAlertController.Style.alert)
        let btnOK = UIAlertAction(title:"好的", style: .default) { (cancelAction)in
            self.setupVisitorView()
        }
        alert.addAction(btnOK)
        self.present(alert, animated: true, completion: nil)
    }
    
    
    // 归档
    func saveWithFile(str : String, plist : String)
    {
           // 1、获得沙盒的根路径
           let home = NSHomeDirectory() as NSString
           // 2、获得Documents路径，使用NSString对象的stringByAppendingPathComponent()方法拼接路径
           let docPath = home.appendingPathComponent("Documents") as NSString
           // 3、获取文本文件路径
           let filePath = docPath.appendingPathComponent(plist)//data.plist
           let dataSource = NSMutableArray()
            dataSource.add(str)
           
           //print(dataSource)
           // 4、将数据写入文件中
           dataSource.write(toFile: filePath, atomically: true)
           print(filePath)
       }
       
    // 解档
    func readWithFile()->(Any)
    {
           /// 1、获得沙盒的根路径
           let home = NSHomeDirectory() as NSString
           /// 2、获得Documents路径，使用NSString对象的stringByAppendingPathComponent()方法拼接路径
           let docPath = home.appendingPathComponent("Documents") as NSString
           /// 3、获取文本文件路径
           let filePath = docPath.appendingPathComponent("ut.plist")
           //let dataSource = NSArray(contentsOfFile: filePath)
           //let dataPath = NSArray(arrayLiteral: filePath)
           let dataSource = NSArray(contentsOfFile: filePath)
           
           //print(dataSource as Any)
           //print(dataPath)
            return dataSource as Any
       }
    
}

